import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '../../../../miniprogram_npm/@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
const $api = require('../../../../request/api').API;
Page({
  data: {
    information: undefined,
    activeParams: undefined,
    activityText: '',
    radio: 1,
    number: 1,
    price: '',
    months: 1
  },
  onLoad: function (options) {
    const { beeperId, btnId } = options;
    this.setData({
      activeParams: {
        beeperId,
        btnId
      }
    });
    this.fetchPropertyPaymentInfo(beeperId, btnId);
    this.fetchActivePreferential(beeperId, btnId, 1);
  },

  /**
   * 获取物业缴费信息
   */
  fetchPropertyPaymentInfo(beeperId, btnId) {
    Toast.loading({
      message: '加载中...',
      forbidClick: true
    });
    $api
      .getPropertyPaymentInfo({
        beeperId,
        btnId
      })
      .then(res => {
        Toast.clear();
        const { pmcExpireDate, pmcLatestDate } = res.data.data;
        if (res.data.data.feePeriod) {
          if (res.data.data.feePeriod.indexOf(',') == -1) {
            res.data.data.feePeriod = [Number(res.data.data.feePeriod)];
          } else {
            res.data.data.feePeriod = res.data.data.feePeriod.split(',').map(val => parseInt(val));
          }
        } else {
          res.data.data.feePeriod = [1];
        }
        this.setData({
          information: {
            ...res.data.data,
            pmcExpireDate: pmcExpireDate.substring(0, 10),
            pmcLatestDate: pmcLatestDate.substring(0, 10)
          }
        });
        this.handleProcessingPrices(1);
      })
      .catch(err => {
        Toast.clear();
        Dialog.alert({
          title: '提示',
          message: err.msg
        }).then(() => {
          wx.navigateBack({
            delta: 1
          });
        });
      });
  },

  /**
   * 获得产品商户的活动优惠内容即活动价
   */
  fetchActivePreferential(beeperId, btnId, months) {
    $api
      .getActivePreferential({
        beeperId,
        btnId,
        months
      })
      .then(res => {
        this.setData({
          activityText: res.data.data
        });
      });
  },

  /**
   * 缴费周期切换
   * @param e
   */
  handlePaymentCycleChange(e) {
    const { beeperId, btnId } = this.data.activeParams;
    let months = 1;
    if (e.detail === 1) months = 1;
    if (e.detail === 2) months = 3;
    if (e.detail === 3) months = 12;
    this.setData({
      radio: e.detail,
      number: 1,
      months
    });
    this.fetchActivePreferential(beeperId, btnId, months);
    this.handleProcessingPrices(months);
  },

  /**
   * 月份数量切换
   * @param e
   */
  handleTextareaInput(e) {
    const { radio } = this.data;
    const { beeperId, btnId } = this.data.activeParams;
    let months = 1;
    if (radio === 1) months = 1 * e.detail;
    if (radio === 2) months = 3 * e.detail;
    if (radio === 3) months = 12 * e.detail;
    this.setData({
      number: e.detail,
      months
    });
    this.fetchActivePreferential(beeperId, btnId, months);
    this.handleProcessingPrices(months);
  },

  /**
   * 处理价格
   */
  handleProcessingPrices(months) {
    const { houseSpace, pmcPrice, pmcSingleDiscount } = this.data.information;
    const price = houseSpace * pmcPrice * months * (pmcSingleDiscount || 1);
    this.setData({
      price: (Math.round((price + Number.EPSILON) * 100) / 100) * 100
    });
  },

  /**
   * 提交数据 缴费
   * @param e
   */
  handleSubmit(e) {
    Toast.loading({ message: '缴费中...', duration: 0, forbidClick: true });
    const { beeperId, btnId } = this.data.activeParams;
    const { months, price } = this.data;
    const params = { beeperId, btnId, months, openId: wx.getStorageSync(Storage.USER_OPEN_ID), amount: price / 100 };
    $api
      .propertyPaymentInfoOrder(params)
      .then(res => {
        const { nonceStr, timeStamp, signType, paySign } = res.data.data;
        wx.requestPayment({
          timeStamp,
          nonceStr,
          package: res.data.data.package,
          signType,
          paySign,
          success: res => {
            Toast.success({
              message: '支付成功',
              forbidClick: true
            });
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/order/index'
              });
            }, 1000);
          },
          fail: err => {
            console.log(err);
            Toast.fail({
              message: '支付失败',
              forbidClick: true
            });
          }
        });
        Toast.clear();
      })
      .catch(() => {
        Toast.clear();
      });
  }
});
